﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Wimm.Common.Interfaces.Utils;

namespace Wimm.Common.Entities.Utils
{
    public class IncomeQuery: IPagedRequest<Income>
    {
        public int PageNumber { get; private set; }

        public int PageSize { get; private set; }

        public string SortKey { get; private set; }

        public string SortOrder { get; private set; }

        public Expression<Func<Income, bool>> Where
        {
            get
            {
                long fromLong = _fromFilter.Value.Date.Ticks;
                long toLong = _toFilter.Value.Date.AddDays(1).AddTicks(-1).Ticks;

                if (_categoryFilter == 0)
                {
                    return (e) => e.Time >= fromLong && e.Time <= toLong && e.UserId == _userId;
                }
                else
                {
                    return (e) => e.Time >= fromLong && e.Time <= toLong && e.UserId == _userId && e.IncomeSourceId == _categoryFilter;
                }
            }
        }

        private DateTimeOffset? _fromFilter;
        private DateTimeOffset? _toFilter;
        private int? _categoryFilter;
        private int _userId;
        
        public IncomeQuery(int pageNumber, int itemsPerPage)
        {
            PageNumber = pageNumber;
            PageSize = itemsPerPage;
        }

        public IncomeQuery(IPagedRequest<Income> pagedQuery, int userId, DateTimeOffset? fromFilter, DateTimeOffset? toFilter, int? categoryFilter)
        {
            this.PageNumber = pagedQuery.PageNumber;
            this.PageSize = pagedQuery.PageSize;
            this.SortKey = pagedQuery.SortKey;
            this.SortOrder = pagedQuery.SortOrder;
            
            _fromFilter = fromFilter;
            _toFilter = toFilter;
            _categoryFilter = categoryFilter;
            _userId = userId;
        }
    }
}
